<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreateGlobalParameters">添加全局参数</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                label: '删除',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <GlobalParametersModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';

  import { useModal } from '@/components/Modal';
  import GlobalParametersModal from './GlobalParametersModal.vue';
  import { columns } from './GlobalParameters.data';
  import { useGlobalParametersStore } from '@/store/modules/globalParameters';
  import { onMounted, ref } from 'vue';
  import { GlobalParameters } from '#/store';

  const globalParametersData = ref();

  defineOptions({ name: 'GlobalParameters' });

  const [registerModal, { openModal }] = useModal();
  const [registerTable, { updateTableDataRecord }] = useTable({
    dataSource: globalParametersData,
    rowKey: 'name',
    columns,
    formConfig: {
      labelWidth: 120,
      // schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    showIndexColumn: false,
    useSearchForm: false,
    showTableSetting: true,
    bordered: true,
    pagination: false,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  /**
   * 创建全局参数
   */
  function handleCreateGlobalParameters() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleDelete(record: Recordable) {
    let name = record.name;
    let type = record.type;
    let afterRemoveGlobalParametersDataList = getGlobalParametersDataList().filter(
      (globalParameters) => {
        let key = globalParameters.name + '___' + globalParameters.type;
        let nowKey = name + '___' + type;
        return key != nowKey;
      },
    );
    const globalParametersStore = useGlobalParametersStore();
    globalParametersStore.setGlobalParametersList(afterRemoveGlobalParametersDataList);

    globalParametersData.value = afterRemoveGlobalParametersDataList;
    console.log(record);
  }

  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      // 演示不刷新表格直接更新内部数据。
      // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
      const result = updateTableDataRecord(values.id, values);
      console.log(result);
    } else {
      console.log('handleSuccess');
      getGlobalParametersDataList();
      console.log(globalParametersData.value);
    }
  }

  /**
   * 获取所有全局配置参数
   */
  function getGlobalParametersDataList(): GlobalParameters[] {
    const globalParametersStore = useGlobalParametersStore();
    globalParametersData.value = globalParametersStore.getGlobalParametersList.filter(
      (globalParameters) => {
        return globalParameters.name != null;
      },
    );
    return globalParametersData.value;
  }
  onMounted(async () => {
    getGlobalParametersDataList();
  });
</script>
